//
//  PageViewController.swift
//  UI
//
//  Created by admin on 14/9/22.
//  Copyright (c) 2014年 sinosun. All rights reserved.
//

import UIKit
let PAGES  = 4
let WIDTH  = 320
let HEIGTH  = 360


class PageViewController: UIViewController, UIScrollViewDelegate {

   // @IBOutlet var sv: UIScrollView!
    
   // @IBOutlet weak var pageControl: UIPageControl!
    
    @IBOutlet var sv: UIScrollView!
    
    @IBOutlet var pageControl: UIPageControl!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        // Do any additional setup after loading the view.
        sv.contentSize = CGSize(width: PAGES * WIDTH, height: HEIGTH)
        sv.pagingEnabled = true
        sv.delegate = self

        sv.backgroundColor = UIColor.grayColor()
//        
        for index in 0...3{
            var imageView_content = UIImageView(image: UIImage(named: String(index)))
            imageView_content.frame = CGRect(x: index * WIDTH, y: 0, width: WIDTH, height: HEIGTH)
            sv.addSubview(imageView_content)
        }
        self.view.addSubview(sv)
//        
        pageControl.numberOfPages = PAGES
        pageControl.currentPage = 0
        pageControl.addTarget(self, action: "turn", forControlEvents: UIControlEvents.ValueChanged)

        
        
        var label : UILabel = UILabel(frame: CGRectMake(10, 64, 300, 56))
        label.text = "悬垂举腿"
        
        self.view.addSubview(label)
        
        
    }

    
    func turn(){
        println("-----------")
        //        var curr_page = (self.pageControl.currentPage + 1) % PAGES
        //        self.pageControl.currentPage = curr_page
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    // MARK: - UIScrollViewDelegate
    func scrollViewDidScroll(scrollView: UIScrollView!){
        var offset = scrollView.contentOffset
        self.pageControl.currentPage = (Int)(offset.x) / 320.0
    } // any offset changes
    
    
    
    @availability(iOS, introduced=3.2)
    func scrollViewDidZoom(scrollView: UIScrollView!) {
        
    }// any zoom scale changes
    
    // called on start of dragging (may require some time and or distance to move)
    func scrollViewWillBeginDragging(scrollView: UIScrollView!){
        println("scrollViewWillBeginDragging")
    }
    // called on finger up if the user dragged. velocity is in points/millisecond. targetContentOffset may be changed to adjust where the scroll view comes to rest
    @availability(iOS, introduced=5.0)
    func scrollViewWillEndDragging(scrollView: UIScrollView!, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>){
        println("scrollViewWillEndDragging")
    }
    // called on finger up if the user dragged. decelerate is true if it will continue moving afterwards
    func scrollViewDidEndDragging(scrollView: UIScrollView!, willDecelerate decelerate: Bool){
        println("scrollViewDidEndDragging")
    }
    
    func scrollViewWillBeginDecelerating(scrollView: UIScrollView!){
        println("scrollViewWillBeginDecelerating")
    } // called on finger up as we are moving
    func scrollViewDidEndDecelerating(scrollView: UIScrollView!){
        println("scrollViewDidEndDecelerating")
    } // called when scroll view grinds to a halt
    
    func scrollViewDidEndScrollingAnimation(scrollView: UIScrollView!) {
        println("scrollViewDidEndScrollingAnimation")
    }// called when setContentOffset/scrollRectVisible:animated: finishes. not called if not animating
    
    func viewForZoomingInScrollView(scrollView: UIScrollView!) -> UIView! {
        return nil
        
    }// return a view that will be scaled. if delegate returns nil, nothing happens
    @availability(iOS, introduced=3.2)
    func scrollViewWillBeginZooming(scrollView: UIScrollView!, withView view: UIView!){
        
    } // called before the scroll view begins zooming its content
    func scrollViewDidEndZooming(scrollView: UIScrollView!, withView view: UIView!, atScale scale: CGFloat) {
        
    }// scale between minimum and maximum. called after any 'bounce' animations
    
    func scrollViewShouldScrollToTop(scrollView: UIScrollView!) -> Bool {
        return true
    }// return a yes if you want to scroll to the top. if not defined, assumes YES
    func scrollViewDidScrollToTop(scrollView: UIScrollView!){
        
    } // called when scrolling animation finished. may be called immediately if already at top


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
